package com.zprmb.udzt.fragment;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONObject;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.zprmb.udzt.R;
import com.zprmb.udzt.adapter.Ex_infoAdapter;
import com.zprmb.udzt.bean.ExBean;
import com.zprmb.udzt.http.AjaxParams;
import com.zprmb.udzt.util.Constant;
import com.zprmb.udzt.util.VolleryUtil;

public class Ex_GoldFragment extends Fragment  implements OnRefreshListener2<ListView>{
	private PullToRefreshListView lv;
	private String token,title;

	private int page=1;
	private ImageView img_none;
	private ListView actualListView;
	private List<ExBean> list=new ArrayList<ExBean>();
	private List<ExBean> list2=new ArrayList<ExBean>();
	private Ex_infoAdapter adapter2;
	@Override
	public void onCreate(@Nullable Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		token=getArguments().getString("token");
		title=getArguments().getString("title");
	}

	@Override
	@Nullable
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view=inflater.inflate(R.layout.ex_fragment, null);
		lv=(PullToRefreshListView)view.findViewById(R.id.lv);
		img_none=(ImageView)view.findViewById(R.id.iv_v4_nonetwork);
		actualListView = lv.getRefreshableView();
		lv.setOnRefreshListener(this);
		lv.setMode(Mode.BOTH);
		initData();
		return view;
	}
	private void initData() {
		// TODO Auto-generated method stub
		AjaxParams params=new AjaxParams();
		if(title.equals("待使用")){
			params.put("state", "UN_USE");
		} else if(title.equals("已使用")){
			params.put("state", "USED");
		}else if(title.equals("已过期")){
			params.put("state", "EXPIRE");
		}
		params.put("pageNo", ""+page);
		params.put("pageSize", "10");
		VolleryUtil volleryUtil=new VolleryUtil() {
			
			@Override
			public void onUpDate(JSONObject jsonObject) {
				// TODO Auto-generated method stub
				Log.e(title, jsonObject.toString());
				JSONObject json1=jsonObject.optJSONObject("data");
				Gson gson=new Gson();
				if(title.equals("待使用")){
					list=gson.fromJson(json1.optString("resultList"), new TypeToken<ArrayList<ExBean>>() {
					}.getType());
					list2.addAll(list);

					if(list2.size()<1){
						lv.setVisibility(View.GONE);
						img_none.setVisibility(View.VISIBLE);
					}else{
						lv.setVisibility(View.VISIBLE);
						img_none.setVisibility(View.GONE);
					}
				}else if(title.equals("已使用")){
					list=gson.fromJson(json1.optString("resultList"), new TypeToken<ArrayList<ExBean>>() {
					}.getType());
					list2.addAll(list);
					adapter2=new Ex_infoAdapter(getActivity(), list2, title);
					lv.setAdapter(adapter2);
					if(list2.size()<1){
						lv.setVisibility(View.GONE);
						img_none.setVisibility(View.VISIBLE);
					}else{
						lv.setVisibility(View.VISIBLE);
						img_none.setVisibility(View.GONE);
					}
				}
				else if(title.equals("已过期")){
					list=gson.fromJson(json1.optString("resultList"), new TypeToken<ArrayList<ExBean>>() {
					}.getType());
					list2.addAll(list);
					adapter2=new Ex_infoAdapter(getActivity(), list2, title);
					lv.setAdapter(adapter2);
					if(list2.size()<1){
						lv.setVisibility(View.GONE);
						img_none.setVisibility(View.VISIBLE);
					}else{
						lv.setVisibility(View.VISIBLE);
						img_none.setVisibility(View.GONE);
					}
				}
			}
			
			@Override
			public void onError() {
				// TODO Auto-generated method stub
				
			}
		};
		volleryUtil.setHeaders("token", token);
		volleryUtil.setRequestMethod(0);
	    volleryUtil.setShow(true);
		volleryUtil.execute(getActivity(), Constant.URL+"/api/v5/experience/listExpByState?", params.toString());
		
		
	}
	Handler handler=new Handler(){
		public void handleMessage(Message msg) {
			list.clear();
			if(msg.what==1){
				page=1;
				list2.clear();
				initData();
			}else{
				page+=1;
				initData();
				
				
			}
			Toast.makeText(getActivity(), "刷新成功", 2000).show();
			lv.onRefreshComplete();
		};
	};
	public static Ex_GoldFragment newInstance(int position, String string,String token) {
		Ex_GoldFragment uf = new Ex_GoldFragment();
		Bundle bundle = new Bundle();
		bundle.putString("token", token);
		bundle.putString("title", string);
		
		uf.setArguments(bundle);
		return uf;
	}

	@Override
	public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
		// TODO Auto-generated method stub
		handler.sendEmptyMessage(1);
	}

	@Override
	public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
		// TODO Auto-generated method stub
		handler.sendEmptyMessage(2);
	}

}
